#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void login_acc(void)
{
	char path[PATH_MAX];
	Account temp;
	sprintf(path,"%s/%d",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDONLY);
	if(0 > fd)
	{
		perror("open acc");
		strcpy(stoc.str,"未找到该用户，请确认账号");
	}
	else
	{
		read(fd,&temp,sizeof(Account));
		close(fd);
		int fd1 = open(path,O_WRONLY|O_TRUNC);
		printf("%d %s %s %f %d %d\n",temp.pass,temp.name,temp.card,temp.money,temp.count,temp.flag);
		if(0 != temp.flag)
		{
			strcpy(stoc.str,"您的账号已被锁定，请先解锁");
		}
	
		else if(temp.pass != ctos.acc.pass)
		{
			
			temp.count -= 1;
			if(0 == temp.count)
			{
				temp.flag = 1;
			}
			strcpy(stoc.str,"账号或密码错误!");
		}
		else
		{
			strcpy(stoc.str,"登陆成功！");
			temp.count = 3;
		}
		write(fd1,&temp,sizeof(Account));
		close(fd1);
		}
	stoc_send(&stoc);
}


int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_LOGIN);
		stoc.type = ctos.pid;
		login_acc();
	}
}
